//
//  MyStatusRetweetView.swift
//  weibo08
//
//  Created by 陈杰明 on 16/3/8.
//  Copyright © 2016年 JM. All rights reserved.
//

import UIKit
import SnapKit

class MyStatusRetweetView: UIView {

    //MARK: - 属性
    private var statusRetweetViewBottom: Constraint?
    //微博模型
    var status: MyStatus? {
        didSet{
            //设置内容
            let name = status?.user?.name ?? "没有名称"
            let text = status?.text ?? "没有微博内容"
            
            retweetContentLabel.text = "@\(name): \(text)"
            //设置配图视图显示的内容
            //判断是否有内容
            statusRetweetViewBottom?.uninstall()
            if let _ = status?.storePictureURL {
            retweetPictureView.hidden = false
                retweetPictureView.status = status
                self.snp_makeConstraints { (make) -> Void in
                    self.statusRetweetViewBottom = make.bottom.equalTo(retweetPictureView).offset(MyStatusCellMargin).constraint
                }
            }else {
                retweetPictureView.hidden = true
                self.snp_makeConstraints { (make) -> Void in
                    self.statusRetweetViewBottom = make.bottom.equalTo(retweetContentLabel).offset(MyStatusCellMargin).constraint
                }
            }
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }

    
    func prepareUI() {
    //添加子控件
        self.addSubview(retweetBkgButton)
        self.addSubview(retweetContentLabel)
        self.addSubview(retweetPictureView)
    //约束
        //按钮
        retweetBkgButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        //文本
        retweetContentLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.equalTo(self).offset(MyStatusCellMargin)
            make.trailing.equalTo(self).offset(-MyStatusCellMargin)
        }
        //图片
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetContentLabel.snp_bottom).offset(MyStatusCellMargin)
            make.leading.equalTo(self).offset(MyStatusCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        //自己高度约束
        self.snp_makeConstraints { (make) -> Void in
            self.statusRetweetViewBottom = make.bottom.equalTo(retweetPictureView).offset(MyStatusCellMargin).constraint
        }
    }
    //MARK: - 懒加载
    //背景按钮
    private var retweetBkgButton: UIButton = {
        
        let button = UIButton()
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
    
        return button
    }()
    //文本内容
    private var retweetContentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), font: 15)
    
        label.numberOfLines = 0
        return label
    }()
    //图片
    private var retweetPictureView: MyStatusPictureView = MyStatusPictureView()
}
